package com.shanyue.redwine.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.shanyue.redwine.R;
import com.shanyue.redwine.databinding.ActivityWineCabinetBinding;
import com.shanyue.redwine.fragment.FragmentWineCabinetGoodsList;
import com.shanyue.redwine.fragment.FragmentWineCabinetOrderList;
import com.shanyue.redwine.fragment.FragmentWineCabinetRecordList;

public class ActivityWineCabinet extends ActivityBase<ActivityWineCabinetBinding> {
    Fragment mFragmentWineCabinetRecord, mFragmentGoodsList, mFragmentOrderList;
    Fragment mLastFragment;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new
            BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction ft = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_msg:
                    if (mFragmentWineCabinetRecord != null) {
                        if (mLastFragment != null) {
                            ft.hide(mLastFragment);
                        }
                        ft.show(mFragmentWineCabinetRecord);
                    } else {
                        mFragmentWineCabinetRecord = FragmentWineCabinetRecordList.newInstance();
                        ft.add(R.id.fragment_container, mFragmentWineCabinetRecord);
                    }
                    mLastFragment = mFragmentWineCabinetRecord;
                    ft.commit();
                    break;
                case R.id.navigation_goods:
                    if (mFragmentGoodsList != null) {
                        if (mLastFragment != null) {
                            ft.hide(mLastFragment);
                        }
                        ft.show(mFragmentGoodsList);
                    } else {
                        mFragmentGoodsList = FragmentWineCabinetGoodsList.newInstance();
                        ft.add(R.id.fragment_container, mFragmentGoodsList);
                    }
                    mLastFragment = mFragmentGoodsList;
                    ft.commit();
                    break;
                case R.id.navigation_order:
                    if (mFragmentOrderList != null) {
                        if (mLastFragment != null) {
                            ft.hide(mLastFragment);
                        }
                        ft.show(mFragmentOrderList);
                    } else {
                        mFragmentOrderList = FragmentWineCabinetOrderList.newInstance();
                        ft.add(R.id.fragment_container, mFragmentOrderList);
                    }
                    mLastFragment = mFragmentOrderList;
                    ft.commit();
                    break;
            }
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(mActivity, R.layout.activity_wine_cabinet);
        mBinding.tbBar.setTitle("美约红酒柜");
        setSupportActionBar(mBinding.tbBar);
        mBinding.tbBar.setNavigationOnClickListener(v -> onBackPressed());
        mBinding.navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        mBinding.navigation.setSelectedItemId(R.id.navigation_msg);
//        FragmentManager fragmentManager = getSupportFragmentManager();
//        FragmentTransaction ft = fragmentManager.beginTransaction();
//        mFragmentWineCabinetRecord = FragmentWineCabinetRecordList.newInstance();
//        ft.add(R.id.fragment_container, mFragmentWineCabinetRecord);
//        ft.commit();
//        mLastFragment = mFragmentWineCabinetRecord;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isForeground = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isForeground = false;
    }
}
